<?php
	namespace PHPLog\Install\Common;

	use PHPUpdater\cHistory;
	use PHPUpdater\cVersion;
	function clean_Path ($str_Path)
	{
		$str_Path = str_replace("\\", "/", $str_Path);
		$str_Path = str_replace("//", "/", $str_Path);
		return $str_Path;
	}

	function create_Directory ($str_Directory)
	{
		$bool_Created = false;
		$obj_Action = $GLOBALS["Install_History_File"]->create_ActionElement();
		$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_DEST, $str_Directory);

		if (!file_exists($str_Directory))
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE_CREATE);

			$int_OldMask = umask(0);
			if (@mkdir($str_Directory, 0777, true))
			{
				$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_SUCCESS);
				$bool_Created = true;
			}
			else
			{
				$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_FAIL);
				$bool_Created = false;
			}
			umask($int_OldMask);
		}
		else
		{
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE_CHECKED);
			$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_SUCCESS);
		}
		$GLOBALS["Install_History"]->appendChild(clone $obj_Action);
		return $bool_Created;
	}

	function open_History ($str_XMLPath)
	{
		$GLOBALS["Install_History"] = null;
		$GLOBALS["Install_History_File"] = null;
		$GLOBALS["Install_History_File"] = new \PHPUpdater\cHistory($str_XMLPath);
		$GLOBALS["Install_History_File"]->load();

		$GLOBALS["Install_History"] = $GLOBALS["Install_History_File"]->get_InstallElement();
	}

	function set_Version (cVersion $obj_Version)
	{
		if (!$GLOBALS["Install_History_File"])
		{
			return false;
		}
		$GLOBALS["Install_History_File"]->set_Version($obj_Version);
		return true;
	}

	function set_App_Name ($str_AppName = "default")
	{
		if (!$GLOBALS["Install_History_File"])
		{
			return false;
		}
		$GLOBALS["Install_History_File"]->set_AppName($str_AppName);
		return true;
	}

	function set_Update_Site ($str_URL = "unknown")
	{
		if (!$GLOBALS["Install_History_File"])
		{
			return false;
		}
		$GLOBALS["Install_History_File"]->set_UpdateURL($str_URL);
		return true;
	}

	function save_History ()
	{
		if (!$GLOBALS["Install_History_File"])
		{
			return;
		}
		$GLOBALS["Install_History_File"]->set_LastUpdate(date("Y-m-d H:i:s"));
		if ($GLOBALS["Install_History"])
		{
			$GLOBALS["Install_History_File"]->set_InstallElement($GLOBALS["Install_History"]);
		}
		$GLOBALS["Install_History_File"]->save();
	}

	function recurse_Copy ($str_Src, $str_Dst)
	{
		set_time_limit(30);

		$obj_Dir = opendir($str_Src);

		create_Directory($str_Dst);
		while (false !== ($obj_File = @readdir($obj_Dir)))
		{
			if (($obj_File != '.') && ($obj_File != '..'))
			{
				if (is_dir($str_Src . '/' . $obj_File))
				{
					if (!recurse_copy($str_Src . '/' . $obj_File, $str_Dst . '/' . $obj_File))
					{
						closedir($obj_Dir);
						return false;
					}
				}
				else
				{
					//print($src . '/' . $file . " -> " . clean_Path($dst . '/' . $file) . " - ");
					set_time_limit(30);
					$obj_Action = $GLOBALS["Install_History_File"]->create_ActionElement();
					$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_DEST, clean_Path($str_Dst . '/' . $obj_File));
					$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_SRC, clean_Path($str_Src . '/' . $obj_File));
					$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_TYPE_COPY);

					if (!@copy($str_Src . '/' . $obj_File, clean_Path($str_Dst . '/' . $obj_File)))
					{
						$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_FAIL);
						$GLOBALS["Install_History"]->appendChild(clone $obj_Action);
						@closedir($obj_Dir);
						//print("Fail.<br/>");
					}
					else
					{
						$obj_Action->setAttribute(\PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS, \PHPUpdater\cHistory::C_STR_ACTION_ATTRIB_STATUS_SUCCESS);
					}
					//print("Succeded.<br/>");
					$GLOBALS["Install_History"]->appendChild(clone $obj_Action);
				}
			}
		}
		@closedir($obj_Dir);
		return true;
	}
?>